﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PsychveyApp1.classes;
using System.Collections;

namespace PsychveyApp1
{
    public partial class AnswerSurveyHome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUsername.Text = (string)(Session["username"]);

        }

        protected void btnEnterSurvey_Click(object sender, EventArgs e)
        {
            Survey s1 = DBManager.GetSurveyById(tbxEnteredSurveyID.Text);
            if (s1.surveyID != null)
            {
                if ((DBManager.GetUserCompleteSurvey(((UserAccount)Session["currentUser"]).userID, s1.surveyID)) == false)
                {   
                    if (Convert.ToDateTime(DBManager.GetExpirationDateOfSurvey(s1.surveyID)) > DateTime.Now)
                    {
                        string surveyid = tbxEnteredSurveyID.Text.Trim();
                        string querystring = "surveyid=" + surveyid;
                        string querystring2 = surveyid;
                        Session["surveyid"] = querystring;
                        Session["displayinfosheetsurveyid"] = querystring2;
                        Survey survey2 = new Survey();
                        survey2.surveyID = surveyid;

                        Session["CurrentPageNumber"] = 1;
                        Response.Redirect("DisplayedInformationSheet.aspx");
                        //Server.TransferRequest("InformationSheet.aspx");
                        //Server.Transfer("AnswerSurvey.aspx?" + querystring);
                    }
                    else if (Convert.ToDateTime(DBManager.GetExpirationDateOfSurvey(s1.surveyID)) <= DateTime.Now)
                    {
                        lblSurveyIDNotFound.Text = "The survey you entered has expired. Please enter another surveyid.";
                    }
                }
                else if ((DBManager.GetUserCompleteSurvey(((UserAccount)Session["currentUser"]).userID, s1.surveyID)) == true)
                {
                    lblSurveyIDNotFound.Text = "You have already completed this survey. Please choose another survey.";
                }
            }
            //Server.TransferRequest("AnswerSurvey.aspx?" + querystring);
            else
            {
                lblSurveyIDNotFound.Text = "SurveyID not found!";
            }
        }

        protected void btnProceed_Click(object sender, EventArgs e)
        {
            if (ddlUser.SelectedIndex == 1)
            {

                Response.Redirect("UserAccountHome.aspx");
            }
            else if (ddlUser.SelectedIndex == 2)
            {
                Response.Redirect("Home.aspx");
            }
        }

        protected void AdminLoginShortcut_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminLogin.aspx");
        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void MySurveyLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("MySurvey.aspx");
        }

        protected void tbxEnteredSurveyID_TextChanged(object sender, EventArgs e)
        {

        }
    }
}